<?php
/**
* @desc 系统用户表单处理
* @author kevin.wang
* @date 2015-01-01 12:00:00
**/
class AdminForm extends BaseForm
{
	public $username;
	public $realname;
	public $password;
	public $email;
	public $mobile;
	public $addr;
	//表单规则
	public function rules()
	{
		return array(
			array('username, realname, email, mobile, addr', 'required', 'message'=>'必填字段不能为空'),
			array('username', 'unique' , 'on'=>'add', 'className'=>'Admin', 'message'=>'用户名已存在'),
			array('password', 'required', 'on'=>'add', 'message'=>'密码不能为空'),
			array('email', 'match', 'pattern'=>"/^(([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?))$/",'message'=>'请输入正确邮箱'),
			array('mobile', 'match', 'pattern'=>"/^((1(([35][0-9])|(47)|[8][01236789]))\d{8})$/",'message'=>'请输入正确手机号')
		);
	}
	//写入数据
	public function saveData(){
		$model = new Admin;
		$data = $this->attributes;
		$data['password'] = $this->encryPasswd($data['password']);
		return $model->insertAdmin($data);
	}
	//修改数据
	public function editData($id){
		$model = new Admin;
		$data = $this->attributes;
		unset($data['password']);
		return $model->editAdmin($id, $data);
	}
	//修改密码
	public function modifyAdminPasswd(array $arr){
		$userInfo = json_decode(Yii::app()->session['userinfo'], true);
		$model = new Admin;
		$userInfo = $model->getAdmin($userInfo['admin_user_id']);
		$passwd = $this->encryPasswd($arr['user_old_pwd']);
		if($passwd != $userInfo['password']){
			$this->addError('error','原密码输入有误');
			return false;
		}
		$newPasswd = $this->encryPasswd($arr['user_new_pwd']);
		if(strlen($newPasswd) < 6){
			$this->addError('error','新密码不能小于 6 位');
			return false;
		}
		$reNewPasswd = $this->encryPasswd($arr['user_confirm_pwd']);
		if($newPasswd != $reNewPasswd){
			$this->addError('error','新密码和确认密码不相等');
			return false;
		}
		return $model->modifyPasswd($newPasswd, $userInfo['admin_user_id']);
	}
}
